package com.etc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.dao.*;
import com.etc.dao.impl.AnsAnswerDaoImpl;
import com.etc.dao.impl.AnsMessageDaoImpl;
import com.etc.entity.*;
import com.etc.service.impl.AnsAnswerServiceDaoImpl;
import com.etc.service.impl.MessageServiceImpl;
import com.etc.service1.AnsAnswerServiceDao;
import com.etc.service1.MessageServiceDao;

/**
 * Servlet implementation class AnsAnswerServlet
 */
@WebServlet("/AnsAnswerServlet")
public class AnsAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AnsAnswerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String aa = request.getParameter("aa");
		//System.out.println(aa);
		if("answer"==aa||"answer".equals(aa)) {
			answeranswer(request,response);
		}
		else if("feng"==aa||"feng".equals(aa)) {
			fenganswer(request,response);
		}
		else if("jie"==aa||"jie".equals(aa)) {
			jieanswer(request,response);
		}
		else if("del"==aa||"del".equals(aa)) {
			delanswer(request,response);
		}
		String type = request.getParameter("type");
	}
	
	protected void answeranswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String username = request.getParameter("username");
		String title = request.getParameter("title");
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		int upid = Integer.parseInt(request.getParameter("upid"));
		
		//System.out.println(username+title+type+content+ansid);
		AnsAnswer ansanswer = new AnsAnswer();
		ansanswer.setTitle(title);
		ansanswer.setUsername(username);
		ansanswer.setType(type);
		ansanswer.setContent(content);
		ansanswer.setAnsid(ansid);
		
		AnsAnswerServiceDao aadao=new AnsAnswerServiceDaoImpl();
		if(aadao.answerAnsMessage(ansanswer))
		{
//			System.out.println("ok");
//			detailmessage(request,response);
			MessageServiceDao dao = new MessageServiceImpl();
			Message message=dao.findMessage(upid);
			AnsMessageDao amdao=new AnsMessageDaoImpl();
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			anslist=amdao.findTitleAnsMessage(upid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		}
		else
		{
			System.out.println("ans-no");
		}
	}

	protected void fenganswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		
		AnsAnswerServiceDao aadao=new AnsAnswerServiceDaoImpl();
		if(aadao.closeAnsAnswer(id))
		{
			//System.out.println("feng-yes");
			int upid = aadao.findUpid(ansid);
			MessageServiceDao dao = new MessageServiceImpl();
			Message message=dao.findMessage(upid);
			
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			AnsMessageDao amdao=new AnsMessageDaoImpl();
			anslist=amdao.findTitleAnsMessage(upid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		}
		else
		{
			System.out.println("feng-no");
		}
	}

	protected void jieanswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		
		AnsAnswerServiceDao aadao=new AnsAnswerServiceDaoImpl();
		if(aadao.openAnsAnswer(id))
		{
			//System.out.println("jie-yes");
			int upid = aadao.findUpid(ansid);
			MessageServiceDao dao = new MessageServiceImpl();
			Message message=dao.findMessage(upid);
			
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			AnsMessageDao amdao = new AnsMessageDaoImpl();
			anslist=amdao.findTitleAnsMessage(upid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		}
		else
		{
			System.out.println("jie-no");
		}
	}
	
	protected void delanswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		String title = request.getParameter("title");
		
		AnsAnswerServiceDao aadao=new AnsAnswerServiceDaoImpl();
		if(aadao.deleteAnsAnswer(id))
		{
			//System.out.println("jie-yes");
			int upid = aadao.findUpid(ansid);
			MessageServiceDao dao = new MessageServiceImpl();
			Message message=dao.findMessage(upid);
			
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			AnsMessageDao amdao = new AnsMessageDaoImpl();
			anslist=amdao.findTitleAnsMessage(upid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		}
		else
		{
			System.out.println("jie-no");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
